﻿using System;
using System.IO;
using System.Drawing;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Globalization;
using System.Text;

namespace VSM
{
    public partial class DealerProfileEdit : System.Web.UI.Page
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session[Utils.AppConstants.ID_User] == null || Session[Utils.AppConstants.Password] == null || Session[Utils.AppConstants.ID_Dealer] == null)
                Response.Redirect("../../Login.aspx");

            if (!IsPostBack)
            {
                if (Request.QueryString["id"] != null && Request.QueryString["id"].Length > 0)
                {
                    txtDealerCode.ReadOnly = true;
                    txtDealerCode.BackColor = ColorTranslator.FromHtml("#E6E6E6");
                    DealerCode.Value = Request.QueryString["id"];
                    btnUpdate.Text = "Update Dealer";
                    if (string.Compare("VSM", (string)Session[Utils.AppConstants.ID_Dealer]) == 0)
                    {
                        LoadData(Utils.AppConstants.VSM, Request.QueryString["id"].ToString());
                        LoadDealerDetails(Utils.AppConstants.VSM, Request.QueryString["id"].ToString());
                    }
                }
                else if (string.Compare("VSM", (string)Session[Utils.AppConstants.ID_Dealer]) == 0) // Add new dealer
                {
                    btnUpdate.Text = "Add Dealer";
                    DealerCode.Value = "";
                }
                // If Biz. Registration Cert. existed --> Load link file. 
                LoadLinkFile();
            }
        }

        /// <summary>
        /// 
        /// </summary>
        private void LoadData(string dbType, string CardCode)
        {
            Transaction trx = new Transaction();
            DataSet dsDealer = trx.GetMarketingDocument_ReturnDS( ConvertDocType.Get2String(DocType.BUSINESS_PARTNER), CardCode, dbType);
            DataTable dtDealer = dsDealer.Tables[0];
            BindData(dtDealer);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="tblData"></param>
        private void BindData(DataTable tblData)
        {
            if (tblData.Rows.Count > 0)
            {
                DataRow dr = tblData.Rows[0];
                txtDealerCode.Text = dr["CardCode"].ToString();
                txtDealerName.Text = dr["CardName"].ToString();
                txtForeignName.Text = dr["CardFName"].ToString();
                txtAddress.Text = dr["Address"].ToString();
                txtTaxCode.Text = dr["LicTradNum"].ToString();
                txtTel1.Text = dr["Phone1"].ToString();
                txtTel2.Text = dr["Phone2"].ToString();
                txtMobilePhone.Text = dr["Cellular"].ToString();
                txtEmail.Text = dr["E_mail"].ToString();
                txtFax.Text = dr["Fax"].ToString();
                // UDF
                txtWebsite.Text = dr["U_Website"].ToString();
                txtBizRegCertificate.Text = dr["U_Biz_RegCertNumber"].ToString();

                if(string.IsNullOrEmpty(dr["U_IssueDate"].ToString()))
                    txtIssueDate.Text = string.Empty;
                else
                    txtIssueDate.Text = Convert.ToDateTime(dr["U_IssueDate"].ToString()).ToString("dd/MM/yyyy");

                txtIssueBy.Text = dr["U_IssueBy"].ToString();
                txtCharterCapital.Text = dr["U_CharterCapital"].ToString();
                txtInvestmentCapital.Text = dr["U_InvestmentCapital"].ToString();
                txtBankAccount.Text = dr["U_BankAccount"].ToString();
                txtBankName.Text = dr["U_BankName"].ToString();
                txtRepresentative.Text = dr["U_Representative"].ToString();
                txtPosition.Text = dr["U_Position"].ToString();
                txtADSLInfo.Text = dr["U_ADSLInfo"].ToString();
                txtUpload.Text = dr["U_UploadSpeed"].ToString();
                txtDownload.Text = dr["U_DownloadSpeed"].ToString();
                //
                string dbType = Session[Utils.AppConstants.ID_Dealer].ToString().Equals(Utils.AppConstants.VSM) ? Utils.AppConstants.VSM : Utils.AppConstants.DEALER;
                GeneralFunctions GF = new GeneralFunctions(dbType, Session[Utils.AppConstants.ID_Dealer].ToString());
                txtProvinceCode.Text = dr["U_ProvinceCode"].ToString();
                txtProvinceName.Text = GF.GetSingleValue(dbType, " select Name from [@PROVINCECODE] where Code = " + dr["U_ProvinceCode"].ToString(), "Name");
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="e"></param>
        protected override void OnLoadComplete(EventArgs e)
        {
            try
            {
                base.OnLoadComplete(e);
                if (this.Request["__EVENTARGUMENT"] != null && this.Request["__EVENTARGUMENT"].ToString() != "")
                {
                    switch (this.Request["__EVENTARGUMENT"].ToString())
                    {
                        case "SelectProvince":
                            this.lblError.Visible = false;
                            this.txtProvinceCode.Text = Session["ChosenCode"] != null ? Session["ChosenCode"].ToString() : string.Empty;
                            this.txtProvinceName.Text = Session["ChosenName"] != null ? Session["ChosenName"].ToString() : string.Empty;
                            Session["ChosenCode"] =
                            Session["ChosenName"] =
                            Session["ChosenProvince"] = null;
                            break;

                        default:
                            break;
                    }
                }
            }
            catch (Exception ex)
            {
                string alert = "alert('Error: " + ex.Message + " ');";
                ScriptManager.RegisterClientScriptBlock(Page, this.GetType(), "CallJS", alert, true);
            }
        }

        private void LoadDealerDetails(string dbType, string CardCode)
        {
            GeneralFunctions GF = new GeneralFunctions(dbType, Session[Utils.AppConstants.ID_Dealer].ToString());
            DataTable tblShowRoom = GF.GetMasterListBySqlCommand(dbType, "select row_number() Over(Order By internal_id) No, * from tbl_ShowRoom where CardCode = '" + CardCode + "'");
            DataTable tblServiceYard = GF.GetMasterListBySqlCommand(dbType, "select row_number() Over(Order By internal_id) No, * from tbl_ServiceYard where CardCode = '" + CardCode + "'");
            DataTable tblMgmtDept = GF.GetMasterListBySqlCommand(dbType, "select row_number() Over(Order By SlpCode) No, SlpCode, SlpName, U_ChucVu, U_NamSinh, U_Trinhdo, U_Sodt, U_Email, U_Manager, U_Code from OSLP where U_Code = '" + CardCode + "' and U_Manager = -9 ");
            DataTable tblGrpCompanies = GF.GetMasterListBySqlCommand(dbType, "select row_number() Over(Order By internal_id) No, * from tbl_GroupCompany where CardCode = '" + CardCode + "'");
            BindDataDetails(tblShowRoom, tblServiceYard, tblMgmtDept, tblGrpCompanies);
        }

        private void BindDataDetails(DataTable tblShoowRoom, DataTable tblServiceYard, DataTable tblMgmtDept, DataTable tblGrpCompanies)
        {
            if (tblShoowRoom == null) tblShoowRoom = CreateShwREmpty();
            if (tblServiceYard == null) tblServiceYard = CreateSvcYardEmpty();
            if (tblGrpCompanies == null) tblGrpCompanies = CreateGrpCompanyEmpty();

            Session["tblShowRoom"] = tblShoowRoom;
            Session["tblServiceYard"] = tblServiceYard;
            Session["tblMgmtDept"] = tblMgmtDept;
            Session["tblGrpCompanies"] = tblGrpCompanies;
            //
            grvShowRoom.DataSource = tblShoowRoom;
            grvShowRoom.DataBind();
            //
            grvServiceYard.DataSource = tblServiceYard;
            grvServiceYard.DataBind();
            //
            GrvMgmtDept.DataSource = tblMgmtDept;
            GrvMgmtDept.DataBind();
            //
            grvGrpCompany.DataSource = tblGrpCompanies;
            grvGrpCompany.DataBind();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            string dbType = Session[Utils.AppConstants.ID_Dealer].ToString().Equals(Utils.AppConstants.VSM) ? Utils.AppConstants.VSM : Utils.AppConstants.DEALER;
            if (string.IsNullOrEmpty(txtDealerCode.Text))
            {
                Session["errorMessage"] = "Missing Dealer Code";
                Session["requestXML"] = string.Empty;
                ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "OKErrors", "Main.setMasterMessage('Missing Dealer Code','');", true);
            }

            String requestXML = _collectData();
            DataSet dsDetails = _collectDataDetails();

            Transaction ts = new Transaction();
            DataSet ds = ts.CreateUpdateDealer(dbType, requestXML, !string.IsNullOrEmpty(DealerCode.Value), txtDealerCode.Text, dsDetails);

            if ((int)ds.Tables[0].Rows[0]["ErrCode"] != 0)
            {
                Session["errorMessage"] = ds.Tables[0].Rows[0]["ErrMsg"];
                Session["requestXML"] = requestXML;
                ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "OKErrors", "Main.setMasterMessage('" + GeneralFunctions.UrlFullEncode(ds.Tables[0].Rows[0]["ErrMsg"].ToString()) + "','');", true);
            }
            else
            {
                Session["successMessage"] = "Operation complete sucessful!";
                ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "OKErrors", "Main.setMasterMessage('" + "Operation complete sucessful!" + "','');", true);
                ClearScreen();
            }
            ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "CloseLoading", "Dialog.hideLoader();", true);
        }

        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void lnkEdit_Click(object sender, EventArgs e)
        {
            try
            {
                GridViewRow gr = (GridViewRow)((DataControlFieldCell)((LinkButton)sender).Parent).Parent;
            }
            catch (Exception ex)
            {
                this.lblError.Text = ex.Message;
                this.lblError.Visible = true;
            }
        }

        private String _collectData()
        {
            try
            {
                GeneralFunctions GF = new GeneralFunctions(Utils.AppConstants.VSM, Session[Utils.AppConstants.ID_Dealer].ToString());
                DataTable dt = new DataTable("OCRD");
                dt.Columns.Add("CardCode");
                dt.Columns.Add("CardFName");
                dt.Columns.Add("CardName");
                dt.Columns.Add("Address");
                dt.Columns.Add("LicTradNum");
                dt.Columns.Add("Phone1");
                dt.Columns.Add("Phone2");
                dt.Columns.Add("CardType");
                dt.Columns.Add("GroupCode");
                dt.Columns.Add("Cellular");
                dt.Columns.Add("Fax");
                dt.Columns.Add("E_Mail");

                // UDF
                dt.Columns.Add("U_Website");
                dt.Columns.Add("U_Biz_RegCertNumber");
                dt.Columns.Add("U_IssueDate");
                dt.Columns.Add("U_IssueBy");
                dt.Columns.Add("U_CharterCapital");
                dt.Columns.Add("U_InvestmentCapital");
                dt.Columns.Add("U_BankAccount");
                dt.Columns.Add("U_BankName");
                dt.Columns.Add("U_Representative");
                dt.Columns.Add("U_Position");
                dt.Columns.Add("U_ADSLInfo");
                dt.Columns.Add("U_UploadSpeed");
                dt.Columns.Add("U_DownloadSpeed");
                dt.Columns.Add("U_ProvinceCode");
                
                DataRow dr = dt.NewRow();

                dr["CardCode"] = txtDealerCode.Text;
                dr["CardFName"] = txtForeignName.Text.Trim();
                dr["CardName"] = txtDealerName.Text;
                dr["Address"] = txtAddress.Text;
                dr["LicTradNum"] = txtTaxCode.Text;
                dr["Phone1"] = txtTel1.Text;
                dr["Phone2"] = txtTel2.Text;
                dr["CardType"] = "C";
                dr["GroupCode"] = "104";
                dr["Cellular"] = txtMobilePhone.Text;
                dr["Fax"] = txtFax.Text;
                dr["E_Mail"] = txtEmail.Text;
                dr["U_Website"] = txtWebsite.Text.Trim();
                dr["U_Biz_RegCertNumber"] = txtBizRegCertificate.Text.Trim();
                if (string.IsNullOrEmpty(txtIssueDate.Text))
                    dr["U_IssueDate"] = DBNull.Value;
                else
                    dr["U_IssueDate"] = Utils.AppConstants.ConvertToDate(txtIssueDate.Text.Trim()).ToString("yyyyMMdd");
                dr["U_IssueBy"] = txtIssueBy.Text.Trim();
                dr["U_CharterCapital"] = string.IsNullOrEmpty(txtCharterCapital.Text.Trim()) ? "0" : GF.ResetFormatNumeric(txtCharterCapital.Text.Trim());
                dr["U_InvestmentCapital"] = string.IsNullOrEmpty(txtInvestmentCapital.Text.Trim()) ? "0" : GF.ResetFormatNumeric(txtInvestmentCapital.Text.Trim());
                dr["U_BankAccount"] = txtBankAccount.Text.Trim();
                dr["U_BankName"] = txtBankName.Text.Trim();
                dr["U_Representative"] = txtRepresentative.Text.Trim();
                dr["U_Position"] = txtPosition.Text.Trim();
                dr["U_ADSLInfo"] = txtADSLInfo.Text.Trim();
                dr["U_UploadSpeed"] = txtUpload.Text.Trim();
                dr["U_DownloadSpeed"] = txtDownload.Text.Trim();
                dr["U_ProvinceCode"] = txtProvinceCode.Text.Trim();

                DocumentXML objInfo = new DocumentXML();
                DataSet ds = new DataSet("DS");

                dt.Rows.Add(dr);

                ds.Tables.Add(dt.Copy());
                return objInfo.ToXMLStringFromDS(((ulong)DocType.BUSINESS_PARTNER).ToString(), ds);
            }
            catch (Exception ex)
            {
                ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "OKErrors", "Main.setMasterMessage('" + GeneralFunctions.UrlFullEncode(ex.ToString()) + "','');", true);
                ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "CloseLoading", "Dialog.hideLoader();", true);
                return "";
            }
        }

        private DataSet _collectDataDetails()
        {
            DataSet ds = new DataSet("OCRD_Details");
            try
            {
                DataTable tblShoowRoom = (DataTable)Session["tblShowRoom"];
                if (tblShoowRoom == null) tblShoowRoom = CreateShwREmpty();
                //
                DataTable tblServiceYard = (DataTable)Session["tblServiceYard"];
                if (tblServiceYard == null) tblServiceYard = CreateSvcYardEmpty();
                //
                DataTable tblGrpCompanies = (DataTable)Session["tblGrpCompanies"];
                if (tblGrpCompanies == null) tblGrpCompanies = CreateGrpCompanyEmpty();
                // create ds Details ...
                tblShoowRoom.TableName = "tbl_ShowRoom";
                tblServiceYard.TableName = "tbl_ServiceYard";
                tblGrpCompanies.TableName = "tbl_GroupCompany";
                ds.Tables.Add(tblShoowRoom.Copy());
                ds.Tables.Add(tblServiceYard.Copy());
                ds.Tables.Add(tblGrpCompanies.Copy());
            }
            catch (Exception ex)
            {
                lblError.Text = ex.Message;
                lblError.Visible = true;
                return null;
            }
            return ds;
        }

        private void ClearScreen()
        { 
            txtIssueDate.Text = DateTime.Now.ToString("dd/MM/yyyy");
            txtCharterCapital.Text =
            txtInvestmentCapital.Text = "0";
            txtDealerCode.Text =
            txtForeignName.Text =
            txtDealerName.Text =
            txtAddress.Text =
            txtTaxCode.Text =
            txtProvinceCode.Text =
            txtProvinceName.Text =
            txtTel1.Text =
            txtTel2.Text =
            txtMobilePhone.Text =
            txtFax.Text =
            txtEmail.Text =
            txtWebsite.Text =
            txtBizRegCertificate.Text =
            txtIssueBy.Text =
            txtBankAccount.Text =
            txtBankName.Text =
            txtRepresentative.Text =
            txtPosition.Text =
            txtADSLInfo.Text =
            txtUpload.Text =
            txtDownload.Text = string.Empty;
            // Binding details ...
            GeneralFunctions GF = new GeneralFunctions(Utils.AppConstants.VSM, Session[Utils.AppConstants.ID_Dealer].ToString());
            DataTable tblMgmtDept = GF.GetMasterListBySqlCommand(Utils.AppConstants.VSM, "select row_number() Over(Order By SlpCode) No, SlpCode, SlpName, U_ChucVu, U_NamSinh, U_Trinhdo, U_Sodt, U_Email, U_Manager, U_Code from OSLP where U_Code = '" + txtDealerCode.Text + "' and U_Manager = -9 ");
            BindDataDetails(null, null, tblMgmtDept, null);
        }

        private DataTable CreateShwREmpty()
        {
            DataTable tblShoowRoom = new DataTable("tbl_ShowRoom");
            tblShoowRoom.Columns.Add("internal_id");
            tblShoowRoom.Columns.Add("ShowRoomName");
            tblShoowRoom.Columns.Add("Address");
            tblShoowRoom.Columns.Add("CardCode");
            tblShoowRoom.Columns.Add("TelFax");

            tblShoowRoom.Columns.Add("ShowRoomArea");
            tblShoowRoom.Columns.Add("ServiceYardArea");
            tblShoowRoom.Columns.Add("GeneralArea");
            tblShoowRoom.Columns.Add("GroundRent");
            tblShoowRoom.Columns.Add("OwnerInfo");
            tblShoowRoom.Columns.Add("LandLeasingPeriod");
            return tblShoowRoom;
        }

        private DataTable CreateSvcYardEmpty()
        {
            DataTable tblServiceYard = new DataTable("tbl_ServiceYard");
            tblServiceYard.Columns.Add("internal_id");
            tblServiceYard.Columns.Add("ServiceYardName");
            tblServiceYard.Columns.Add("Address");
            tblServiceYard.Columns.Add("CardCode");
            tblServiceYard.Columns.Add("TelFax");
            tblServiceYard.Columns.Add("ServiceYardArea4VSM");
            tblServiceYard.Columns.Add("GeneralArea");
            tblServiceYard.Columns.Add("GroundRent");
            tblServiceYard.Columns.Add("OwnerInfo");
            tblServiceYard.Columns.Add("LandLeasingPeriod");
            return tblServiceYard;
        }

        private DataTable CreateGrpCompanyEmpty()
        {
            DataTable tblGrpCompanies = new DataTable("tbl_GroupCompany");
            tblGrpCompanies.Columns.Add("internal_id");
            tblGrpCompanies.Columns.Add("GrpName");
            tblGrpCompanies.Columns.Add("Place");
            tblGrpCompanies.Columns.Add("CardCode");
            return tblGrpCompanies;
        }

        protected void btnAddShowRoom_Click(object sender, EventArgs e)
        {
            DataTable dtShowRoom = (DataTable)Session["tblShowRoom"];
            DataRow newRow = dtShowRoom.NewRow();
            newRow["No"] = dtShowRoom.Rows.Count + 1;
            newRow["internal_id"] = 0;
            newRow["CardCode"] = txtDealerCode.Text;
            newRow["ShowRoomName"] =
            newRow["Address"] =
            newRow["TelFax"] =
            newRow["ShowRoomArea"] =
            newRow["ServiceYardArea"] =
            newRow["GeneralArea"] =
            newRow["GroundRent"] =
            newRow["OwnerInfo"] =
            newRow["LandLeasingPeriod"] = string.Empty;
            dtShowRoom.Rows.Add(newRow);
            // Binding
            Session["tblShowRoom"] = dtShowRoom;
            grvShowRoom.DataSource = dtShowRoom;
            grvShowRoom.DataBind();
        }

        protected void btnAddServiceYard_Click(object sender, EventArgs e)
        {
            DataTable dtServiceYard = (DataTable)Session["tblServiceYard"];
            DataRow newRow = dtServiceYard.NewRow();
            newRow["No"] = dtServiceYard.Rows.Count + 1;
            newRow["internal_id"] = 0;
            newRow["CardCode"] = txtDealerCode.Text;
            newRow["ServiceYardName"] =
            newRow["Address"] =
            newRow["TelFax"] =
            newRow["ServiceYardArea4VSM"] =
            newRow["GeneralArea"] =
            newRow["GroundRent"] =
            newRow["OwnerInfo"] =
            newRow["LandLeasingPeriod"] = string.Empty;
            dtServiceYard.Rows.Add(newRow);
            // Binding
            Session["tblServiceYard"] = dtServiceYard;
            grvServiceYard.DataSource = dtServiceYard;
            grvServiceYard.DataBind();
        }

        protected void btnAddMgmtDept_Click(object sender, EventArgs e)
        {
            DataTable dtMgmtDept = (DataTable)Session["tblMgmtDept"];
            DataRow newRow = dtMgmtDept.NewRow();
            newRow["No"] = dtMgmtDept.Rows.Count + 1;
            newRow["SlpCode"] = 0;
            newRow["U_Manager"] = -9;
            newRow["U_Code"] = txtDealerCode.Text;
            newRow["SlpName"] =
            newRow["U_ChucVu"] =
            newRow["U_Trinhdo"] =
            newRow["U_Sodt"] =
            newRow["U_Email"] = string.Empty;
            newRow["U_NamSinh"] = 0;
            dtMgmtDept.Rows.Add(newRow);
            // Binding
            Session["tblMgmtDept"] = dtMgmtDept;
            GrvMgmtDept.DataSource = dtMgmtDept;
            GrvMgmtDept.DataBind();
        }

        protected void btnAddGrpCompany_Click(object sender, EventArgs e)
        {
            DataTable dtGrpCompanies = (DataTable)Session["tblGrpCompanies"];
            DataRow newRow = dtGrpCompanies.NewRow();
            newRow["No"] = dtGrpCompanies.Rows.Count + 1;
            newRow["internal_id"] = 0;
            newRow["CardCode"] = txtDealerCode.Text;
            newRow["GrpName"] =
            newRow["Place"] = string.Empty;
            dtGrpCompanies.Rows.Add(newRow);
            // Binding
            Session["tblGrpCompanies"] = dtGrpCompanies;
            grvGrpCompany.DataSource = dtGrpCompanies;
            grvGrpCompany.DataBind();
        }

        private void LoadLinkFile()
        {
            string[] Files = Directory.GetFiles(Server.MapPath("~/SupportDocs/BizRegistrationCert/"));
            string FileName = string.Empty;

            for(int i = 0; i < Files.Length; i++)
            {
                if(Files[i].IndexOf(txtDealerCode.Text.Trim()) > 0)
                {
                    FileName = Path.GetFileName(Files[i]);
                }
            }
            if (FileName.Length > 0)
            {
                this.HyperLink_FileDownload.Text = "Click here download " + FileName;
                this.HyperLink_FileDownload.NavigateUrl = Server.MapPath("~/SupportDocs/BizRegistrationCert/") + FileName;
                this.HyperLink_FileDownload.Visible = true;
            }
        }

        protected void btnUpload_Click(object sender, EventArgs e)
        {
            try
            {
                string FileExt = Path.GetExtension(FileUploadControl_1.FileName);
                string FileName = Server.MapPath("~/SupportDocs/BizRegistrationCert/" + txtDealerCode.Text + "." + FileExt);
                //Save file to folder
                if (!string.IsNullOrEmpty(FileName))
                {
                    FileUploadControl_1.SaveAs(FileName);
                    LoadLinkFile();
                }
            }
            catch (Exception ex)
            {
                lblError.Text = ex.Message;
                lblError.Visible = true;
            }
        }

        #region Show Room Tab --> TextChanged Events

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void txtShowRoomName_OnTextChanged(object sender, EventArgs e)
        {
            try
            {
                this.lblError.Visible = false;
                GridViewRow row = (GridViewRow)((DataControlFieldCell)((TextBox)sender).Parent).Parent;
                ViewState["rowindex"] = row.RowIndex;
                TextBox txtShowRoomName = (TextBox)row.FindControl("txtShowRoomName");
                if (txtShowRoomName.Text.Trim().Length == 0)
                {
                    ScriptManager.RegisterClientScriptBlock(Page, this.GetType(), "CallJS", "alert('Pls input Show Room Name.');", true);
                    txtShowRoomName.Focus();
                    return;
                }

                Label lblNo = (Label)row.FindControl("lblNo");
                DataTable tblShoowRoom = (DataTable)Session["tblShowRoom"];

                if (tblShoowRoom != null)
                {
                    DataRow[] rupdate = tblShoowRoom.Select("No=" + lblNo.Text);
                    if (rupdate.Length > 0) rupdate[0]["ShowRoomName"] = txtShowRoomName.Text.Trim();
                }
                //Binding ...
                this.grvShowRoom.EditIndex = -1;
                Session["tblShowRoom"] = tblShoowRoom;
                this.grvShowRoom.DataSource = tblShoowRoom;
                this.grvShowRoom.DataBind();
            }
            catch (Exception ex)
            {
                this.lblError.Text = ex.Message;
                this.lblError.Visible = true;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void txtAddressShwR_OnTextChanged(object sender, EventArgs e)
        {
            try
            {
                this.lblError.Visible = false;
                GridViewRow row = (GridViewRow)((DataControlFieldCell)((TextBox)sender).Parent).Parent;
                ViewState["rowindex"] = row.RowIndex;
                TextBox txtAddress = (TextBox)row.FindControl("txtAddress");
                if (txtAddress.Text.Trim().Length == 0)
                {
                    ScriptManager.RegisterClientScriptBlock(Page, this.GetType(), "CallJS", "alert('Pls input Address.');", true);
                    txtAddress.Focus();
                    return;
                }

                Label lblNo = (Label)row.FindControl("lblNo");
                DataTable tblShoowRoom = (DataTable)Session["tblShowRoom"];

                if (tblShoowRoom != null)
                {
                    DataRow[] rupdate = tblShoowRoom.Select("No=" + lblNo.Text);
                    if (rupdate.Length > 0) rupdate[0]["Address"] = txtAddress.Text.Trim();
                }
                //Binding ...
                this.grvShowRoom.EditIndex = -1;
                Session["tblShowRoom"] = tblShoowRoom;
                this.grvShowRoom.DataSource = tblShoowRoom;
                this.grvShowRoom.DataBind();
            }
            catch (Exception ex)
            {
                this.lblError.Text = ex.Message;
                this.lblError.Visible = true;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void txtTelFaxShwR_OnTextChanged(object sender, EventArgs e)
        {
            try
            {
                this.lblError.Visible = false;
                GridViewRow row = (GridViewRow)((DataControlFieldCell)((TextBox)sender).Parent).Parent;
                ViewState["rowindex"] = row.RowIndex;
                TextBox txtTelFax = (TextBox)row.FindControl("txtTelFax");

                Label lblNo = (Label)row.FindControl("lblNo");
                DataTable tblShoowRoom = (DataTable)Session["tblShowRoom"];

                if (tblShoowRoom != null)
                {
                    DataRow[] rupdate = tblShoowRoom.Select("No=" + lblNo.Text);
                    if (rupdate.Length > 0 && txtTelFax != null) rupdate[0]["TelFax"] = txtTelFax.Text.Trim();
                }
                //Binding ...
                this.grvShowRoom.EditIndex = -1;
                Session["tblShowRoom"] = tblShoowRoom;
                this.grvShowRoom.DataSource = tblShoowRoom;
                this.grvShowRoom.DataBind();
            }
            catch (Exception ex)
            {
                this.lblError.Text = ex.Message;
                this.lblError.Visible = true;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void txtShowRoomArea_OnTextChanged(object sender, EventArgs e)
        {
            try
            {
                this.lblError.Visible = false;
                GridViewRow row = (GridViewRow)((DataControlFieldCell)((TextBox)sender).Parent).Parent;
                ViewState["rowindex"] = row.RowIndex;
                TextBox txtShowRoomArea = (TextBox)row.FindControl("txtShowRoomArea");

                Label lblNo = (Label)row.FindControl("lblNo");
                DataTable tblShoowRoom = (DataTable)Session["tblShowRoom"];

                if (tblShoowRoom != null)
                {
                    DataRow[] rupdate = tblShoowRoom.Select("No=" + lblNo.Text);
                    if (rupdate.Length > 0 && txtShowRoomArea != null) rupdate[0]["ShowRoomArea"] = txtShowRoomArea.Text.Trim();
                }
                //Binding ...
                this.grvShowRoom.EditIndex = -1;
                Session["tblShowRoom"] = tblShoowRoom;
                this.grvShowRoom.DataSource = tblShoowRoom;
                this.grvShowRoom.DataBind();
            }
            catch (Exception ex)
            {
                this.lblError.Text = ex.Message;
                this.lblError.Visible = true;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void txtServiceYardAreaShwR_OnTextChanged(object sender, EventArgs e)
        {
            try
            {
                this.lblError.Visible = false;
                GridViewRow row = (GridViewRow)((DataControlFieldCell)((TextBox)sender).Parent).Parent;
                ViewState["rowindex"] = row.RowIndex;
                TextBox txtServiceYardArea = (TextBox)row.FindControl("txtServiceYardArea");

                Label lblNo = (Label)row.FindControl("lblNo");
                DataTable tblShoowRoom = (DataTable)Session["tblShowRoom"];

                if (tblShoowRoom != null)
                {
                    DataRow[] rupdate = tblShoowRoom.Select("No=" + lblNo.Text);
                    if (rupdate.Length > 0 && txtServiceYardArea != null) rupdate[0]["ServiceYardArea"] = txtServiceYardArea.Text.Trim();
                }
                //Binding ...
                this.grvShowRoom.EditIndex = -1;
                Session["tblShowRoom"] = tblShoowRoom;
                this.grvShowRoom.DataSource = tblShoowRoom;
                this.grvShowRoom.DataBind();
            }
            catch (Exception ex)
            {
                this.lblError.Text = ex.Message;
                this.lblError.Visible = true;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void txtGeneralAreaShwR_OnTextChanged(object sender, EventArgs e)
        {
            try
            {
                this.lblError.Visible = false;
                GridViewRow row = (GridViewRow)((DataControlFieldCell)((TextBox)sender).Parent).Parent;
                ViewState["rowindex"] = row.RowIndex;
                TextBox txtGeneralArea = (TextBox)row.FindControl("txtGeneralArea");

                Label lblNo = (Label)row.FindControl("lblNo");
                DataTable tblShoowRoom = (DataTable)Session["tblShowRoom"];

                if (tblShoowRoom != null)
                {
                    DataRow[] rupdate = tblShoowRoom.Select("No=" + lblNo.Text);
                    if (rupdate.Length > 0 && txtGeneralArea != null) rupdate[0]["GeneralArea"] = txtGeneralArea.Text.Trim();
                }
                //Binding ...
                this.grvShowRoom.EditIndex = -1;
                Session["tblShowRoom"] = tblShoowRoom;
                this.grvShowRoom.DataSource = tblShoowRoom;
                this.grvShowRoom.DataBind();
            }
            catch (Exception ex)
            {
                this.lblError.Text = ex.Message;
                this.lblError.Visible = true;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void txtGroundRentShwR_OnTextChanged(object sender, EventArgs e)
        {
            try
            {
                this.lblError.Visible = false;
                GridViewRow row = (GridViewRow)((DataControlFieldCell)((TextBox)sender).Parent).Parent;
                ViewState["rowindex"] = row.RowIndex;
                TextBox txtGroundRent = (TextBox)row.FindControl("txtGroundRent");

                Label lblNo = (Label)row.FindControl("lblNo");
                DataTable tblShoowRoom = (DataTable)Session["tblShowRoom"];

                if (tblShoowRoom != null)
                {
                    DataRow[] rupdate = tblShoowRoom.Select("No=" + lblNo.Text);
                    if (rupdate.Length > 0 && txtGroundRent != null) rupdate[0]["GroundRent"] = txtGroundRent.Text.Trim();
                }
                //Binding ...
                this.grvShowRoom.EditIndex = -1;
                Session["tblShowRoom"] = tblShoowRoom;
                this.grvShowRoom.DataSource = tblShoowRoom;
                this.grvShowRoom.DataBind();
            }
            catch (Exception ex)
            {
                this.lblError.Text = ex.Message;
                this.lblError.Visible = true;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void txtOwnerInfoShwR_OnTextChanged(object sender, EventArgs e)
        {
            try
            {
                this.lblError.Visible = false;
                GridViewRow row = (GridViewRow)((DataControlFieldCell)((TextBox)sender).Parent).Parent;
                ViewState["rowindex"] = row.RowIndex;
                TextBox txtOwnerInfo = (TextBox)row.FindControl("txtOwnerInfo");

                Label lblNo = (Label)row.FindControl("lblNo");
                DataTable tblShoowRoom = (DataTable)Session["tblShowRoom"];

                if (tblShoowRoom != null)
                {
                    DataRow[] rupdate = tblShoowRoom.Select("No=" + lblNo.Text);
                    if (rupdate.Length > 0 && txtOwnerInfo != null) rupdate[0]["OwnerInfo"] = txtOwnerInfo.Text.Trim();
                }
                //Binding ...
                this.grvShowRoom.EditIndex = -1;
                Session["tblShowRoom"] = tblShoowRoom;
                this.grvShowRoom.DataSource = tblShoowRoom;
                this.grvShowRoom.DataBind();
            }
            catch (Exception ex)
            {
                this.lblError.Text = ex.Message;
                this.lblError.Visible = true;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void txtLandLeasingPeriodShwR_OnTextChanged(object sender, EventArgs e)
        {
            try
            {
                this.lblError.Visible = false;
                GridViewRow row = (GridViewRow)((DataControlFieldCell)((TextBox)sender).Parent).Parent;
                ViewState["rowindex"] = row.RowIndex;
                TextBox txtLandLeasingPeriod = (TextBox)row.FindControl("txtLandLeasingPeriod");

                Label lblNo = (Label)row.FindControl("lblNo");
                DataTable tblShoowRoom = (DataTable)Session["tblShowRoom"];

                if (tblShoowRoom != null)
                {
                    DataRow[] rupdate = tblShoowRoom.Select("No=" + lblNo.Text);
                    if (rupdate.Length > 0 && txtLandLeasingPeriod != null) rupdate[0]["LandLeasingPeriod"] = txtLandLeasingPeriod.Text.Trim();
                }
                //Binding ...
                this.grvShowRoom.EditIndex = -1;
                Session["tblShowRoom"] = tblShoowRoom;
                this.grvShowRoom.DataSource = tblShoowRoom;
                this.grvShowRoom.DataBind();
            }
            catch (Exception ex)
            {
                this.lblError.Text = ex.Message;
                this.lblError.Visible = true;
            }
        }

        #endregion

        #region Service Yard Tab --> TextChanged Events

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void txtServiceYardName_OnTextChanged(object sender, EventArgs e)
        {
            try
            {
                this.lblError.Visible = false;
                GridViewRow row = (GridViewRow)((DataControlFieldCell)((TextBox)sender).Parent).Parent;
                ViewState["rowindex"] = row.RowIndex;
                TextBox txtServiceYardName = (TextBox)row.FindControl("txtServiceYardName");
                if (txtServiceYardName.Text.Trim().Length == 0)
                {
                    ScriptManager.RegisterClientScriptBlock(Page, this.GetType(), "CallJS", "alert('Pls input Service Yard Name.');", true);
                    txtServiceYardName.Focus();
                    return;
                }

                Label lblNo = (Label)row.FindControl("lblNo");
                DataTable tblServiceYard = (DataTable)Session["tblServiceYard"];

                if (tblServiceYard != null)
                {
                    DataRow[] rupdate = tblServiceYard.Select("No=" + lblNo.Text);
                    if (rupdate.Length > 0) rupdate[0]["ServiceYardName"] = txtServiceYardName.Text.Trim();
                }
                //Binding ...
                this.grvServiceYard.EditIndex = -1;
                Session["tblServiceYard"] = tblServiceYard;
                this.grvServiceYard.DataSource = tblServiceYard;
                this.grvServiceYard.DataBind();
            }
            catch (Exception ex)
            {
                this.lblError.Text = ex.Message;
                this.lblError.Visible = true;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void txtAddressSvcYard_OnTextChanged(object sender, EventArgs e)
        {
            try
            {
                this.lblError.Visible = false;
                GridViewRow row = (GridViewRow)((DataControlFieldCell)((TextBox)sender).Parent).Parent;
                ViewState["rowindex"] = row.RowIndex;
                TextBox txtAddress = (TextBox)row.FindControl("txtAddress");
                if (txtAddress.Text.Trim().Length == 0)
                {
                    ScriptManager.RegisterClientScriptBlock(Page, this.GetType(), "CallJS", "alert('Pls input Address Service Yard.');", true);
                    txtAddress.Focus();
                    return;
                }

                Label lblNo = (Label)row.FindControl("lblNo");
                DataTable tblServiceYard = (DataTable)Session["tblServiceYard"];

                if (tblServiceYard != null)
                {
                    DataRow[] rupdate = tblServiceYard.Select("No=" + lblNo.Text);
                    if (rupdate.Length > 0) rupdate[0]["Address"] = txtAddress.Text.Trim();
                }
                //Binding ...
                this.grvServiceYard.EditIndex = -1;
                Session["tblServiceYard"] = tblServiceYard;
                this.grvServiceYard.DataSource = tblServiceYard;
                this.grvServiceYard.DataBind();
            }
            catch (Exception ex)
            {
                this.lblError.Text = ex.Message;
                this.lblError.Visible = true;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void txtTelFaxSvcYard_OnTextChanged(object sender, EventArgs e)
        {
            try
            {
                this.lblError.Visible = false;
                GridViewRow row = (GridViewRow)((DataControlFieldCell)((TextBox)sender).Parent).Parent;
                ViewState["rowindex"] = row.RowIndex;
                TextBox txtTelFax = (TextBox)row.FindControl("txtTelFax");

                Label lblNo = (Label)row.FindControl("lblNo");
                DataTable tblServiceYard = (DataTable)Session["tblServiceYard"];

                if (tblServiceYard != null)
                {
                    DataRow[] rupdate = tblServiceYard.Select("No=" + lblNo.Text);
                    if (rupdate.Length > 0 && txtTelFax != null) rupdate[0]["TelFax"] = txtTelFax.Text.Trim();
                }
                //Binding ...
                this.grvServiceYard.EditIndex = -1;
                Session["tblServiceYard"] = tblServiceYard;
                this.grvServiceYard.DataSource = tblServiceYard;
                this.grvServiceYard.DataBind();
            }
            catch (Exception ex)
            {
                this.lblError.Text = ex.Message;
                this.lblError.Visible = true;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void txtServiceYardArea4VSM_OnTextChanged(object sender, EventArgs e)
        {
            try
            {
                this.lblError.Visible = false;
                GridViewRow row = (GridViewRow)((DataControlFieldCell)((TextBox)sender).Parent).Parent;
                ViewState["rowindex"] = row.RowIndex;
                TextBox txtServiceYardArea4VSM = (TextBox)row.FindControl("txtServiceYardArea4VSM");

                Label lblNo = (Label)row.FindControl("lblNo");
                DataTable tblServiceYard = (DataTable)Session["tblServiceYard"];

                if (tblServiceYard != null)
                {
                    DataRow[] rupdate = tblServiceYard.Select("No=" + lblNo.Text);
                    if (rupdate.Length > 0 && txtServiceYardArea4VSM != null) rupdate[0]["ServiceYardArea4VSM"] = txtServiceYardArea4VSM.Text.Trim();
                }
                //Binding ...
                this.grvServiceYard.EditIndex = -1;
                Session["tblServiceYard"] = tblServiceYard;
                this.grvServiceYard.DataSource = tblServiceYard;
                this.grvServiceYard.DataBind();
            }
            catch (Exception ex)
            {
                this.lblError.Text = ex.Message;
                this.lblError.Visible = true;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void txtGeneralAreaSvcYard_OnTextChanged(object sender, EventArgs e)
        {
            try
            {
                this.lblError.Visible = false;
                GridViewRow row = (GridViewRow)((DataControlFieldCell)((TextBox)sender).Parent).Parent;
                ViewState["rowindex"] = row.RowIndex;
                TextBox txtGeneralAreaSvcYard = (TextBox)row.FindControl("txtGeneralArea");

                Label lblNo = (Label)row.FindControl("lblNo");
                DataTable tblServiceYard = (DataTable)Session["tblServiceYard"];

                if (tblServiceYard != null)
                {
                    DataRow[] rupdate = tblServiceYard.Select("No=" + lblNo.Text);
                    if (rupdate.Length > 0 && txtGeneralAreaSvcYard != null) rupdate[0]["GeneralArea"] = txtGeneralAreaSvcYard.Text.Trim();
                }
                //Binding ...
                this.grvServiceYard.EditIndex = -1;
                Session["tblServiceYard"] = tblServiceYard;
                this.grvServiceYard.DataSource = tblServiceYard;
                this.grvServiceYard.DataBind();
            }
            catch (Exception ex)
            {
                this.lblError.Text = ex.Message;
                this.lblError.Visible = true;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void txtGroundRentSvcYard_OnTextChanged(object sender, EventArgs e)
        {
            try
            {
                this.lblError.Visible = false;
                GridViewRow row = (GridViewRow)((DataControlFieldCell)((TextBox)sender).Parent).Parent;
                ViewState["rowindex"] = row.RowIndex;
                TextBox txtGroundRent = (TextBox)row.FindControl("txtGroundRent");

                Label lblNo = (Label)row.FindControl("lblNo");
                DataTable tblServiceYard = (DataTable)Session["tblServiceYard"];

                if (tblServiceYard != null)
                {
                    DataRow[] rupdate = tblServiceYard.Select("No=" + lblNo.Text);
                    if (rupdate.Length > 0 && txtGroundRent != null) rupdate[0]["GroundRent"] = txtGroundRent.Text.Trim();
                }
                //Binding ...
                this.grvServiceYard.EditIndex = -1;
                Session["tblServiceYard"] = tblServiceYard;
                this.grvServiceYard.DataSource = tblServiceYard;
                this.grvServiceYard.DataBind();
            }
            catch (Exception ex)
            {
                this.lblError.Text = ex.Message;
                this.lblError.Visible = true;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void txtOwnerInfoSvcYard_OnTextChanged(object sender, EventArgs e)
        {
            try
            {
                this.lblError.Visible = false;
                GridViewRow row = (GridViewRow)((DataControlFieldCell)((TextBox)sender).Parent).Parent;
                ViewState["rowindex"] = row.RowIndex;
                TextBox txtOwnerInfo = (TextBox)row.FindControl("txtOwnerInfo");

                Label lblNo = (Label)row.FindControl("lblNo");
                DataTable tblServiceYard = (DataTable)Session["tblServiceYard"];

                if (tblServiceYard != null)
                {
                    DataRow[] rupdate = tblServiceYard.Select("No=" + lblNo.Text);
                    if (rupdate.Length > 0 && txtOwnerInfo != null) rupdate[0]["OwnerInfo"] = txtOwnerInfo.Text.Trim();
                }
                //Binding ...
                this.grvServiceYard.EditIndex = -1;
                Session["tblServiceYard"] = tblServiceYard;
                this.grvServiceYard.DataSource = tblServiceYard;
                this.grvServiceYard.DataBind();
            }
            catch (Exception ex)
            {
                this.lblError.Text = ex.Message;
                this.lblError.Visible = true;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void txtLandLeasingPeriodSvcYard_OnTextChanged(object sender, EventArgs e)
        {
            try
            {
                this.lblError.Visible = false;
                GridViewRow row = (GridViewRow)((DataControlFieldCell)((TextBox)sender).Parent).Parent;
                ViewState["rowindex"] = row.RowIndex;
                TextBox txtLandLeasingPeriod = (TextBox)row.FindControl("txtLandLeasingPeriod");

                Label lblNo = (Label)row.FindControl("lblNo");
                DataTable tblServiceYard = (DataTable)Session["tblServiceYard"];

                if (tblServiceYard != null)
                {
                    DataRow[] rupdate = tblServiceYard.Select("No=" + lblNo.Text);
                    if (rupdate.Length > 0 && txtLandLeasingPeriod != null) rupdate[0]["LandLeasingPeriod"] = txtLandLeasingPeriod.Text.Trim();
                }
                //Binding ...
                this.grvServiceYard.EditIndex = -1;
                Session["tblServiceYard"] = tblServiceYard;
                this.grvServiceYard.DataSource = tblServiceYard;
                this.grvServiceYard.DataBind();
            }
            catch (Exception ex)
            {
                this.lblError.Text = ex.Message;
                this.lblError.Visible = true;
            }
        }

        #endregion

        #region Group Company Tab --> TextChanged Events
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void txtGrpName_OnTextChanged(object sender, EventArgs e)
        {
            try
            {
                this.lblError.Visible = false;
                GridViewRow row = (GridViewRow)((DataControlFieldCell)((TextBox)sender).Parent).Parent;
                ViewState["rowindex"] = row.RowIndex;
                TextBox txtGrpName = (TextBox)row.FindControl("txtGrpName");
                if (txtGrpName.Text.Trim().Length == 0)
                {
                    ScriptManager.RegisterClientScriptBlock(Page, this.GetType(), "CallJS", "alert('Pls input Group Copany Name.');", true);
                    txtGrpName.Focus();
                    return;
                }

                Label lblNo = (Label)row.FindControl("lblNo");
                DataTable tblGrpCompanies = (DataTable)Session["tblGrpCompanies"];

                if (tblGrpCompanies != null)
                {
                    DataRow[] rupdate = tblGrpCompanies.Select("No=" + lblNo.Text);
                    if (rupdate.Length > 0)
                    {
                        rupdate[0]["GrpName"] = txtGrpName.Text.Trim();
                    }
                }
                //Binding ...
                this.grvGrpCompany.EditIndex = -1;
                Session["tblGrpCompanies"] = tblGrpCompanies;
                this.grvGrpCompany.DataSource = tblGrpCompanies;
                this.grvGrpCompany.DataBind();
            }
            catch (Exception ex)
            {
                this.lblError.Text = ex.Message;
                this.lblError.Visible = true;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void txtPlace_OnTextChanged(object sender, EventArgs e)
        {
            try
            {
                this.lblError.Visible = false;
                GridViewRow row = (GridViewRow)((DataControlFieldCell)((TextBox)sender).Parent).Parent;
                ViewState["rowindex"] = row.RowIndex;
                TextBox txtPlace = (TextBox)row.FindControl("txtPlace");
                if (txtPlace.Text.Trim().Length == 0)
                {
                    ScriptManager.RegisterClientScriptBlock(Page, this.GetType(), "CallJS", "alert('Pls input Place');", true);
                    txtPlace.Focus();
                    return;
                }

                Label lblNo = (Label)row.FindControl("lblNo");
                DataTable tblGrpCompanies = (DataTable)Session["tblGrpCompanies"];

                if (tblGrpCompanies != null)
                {
                    DataRow[] rupdate = tblGrpCompanies.Select("No=" + lblNo.Text);
                    if (rupdate.Length > 0)
                    {
                        rupdate[0]["Place"] = txtPlace.Text.Trim();
                    }
                }
                //Binding ...
                this.grvGrpCompany.EditIndex = -1;
                Session["tblGrpCompanies"] = tblGrpCompanies;
                this.grvGrpCompany.DataSource = tblGrpCompanies;
                this.grvGrpCompany.DataBind();
            }
            catch (Exception ex)
            {
                this.lblError.Text = ex.Message;
                this.lblError.Visible = true;
            }
        }
        #endregion

    }
}